﻿using System;
using System.Globalization;
using System.Windows.Forms;
using DesignerHelper.Main;
using SpreadsheetGear;

namespace WorkbookDesigner.Addons.WorkbookExplorer
{
    public partial class WorkbookSetOptions : UserControl
    {
        public WorkbookSetOptions()
        {
            InitializeComponent();
        }

        public WorkbookSetOptions(IWorkbookSet wbs) : this()
        {
            TriggerInit(wbs);
        }

        private IWorkbookSet BookSet { get; set; }
        private LockBookWorker LbwTrigger { get; set; }

        #region 封装变量

        private Calculation SelectedCalculation
        {
            get
            {
                if (SemiAutomatic.Checked)
                    return Calculation.SemiAutomatic;
                if (Manual.Checked)
                    return Calculation.Manual;
                if (Automatic.Checked)
                    return Calculation.Automatic;
                return Calculation.Automatic;
            }
        }

        private bool SelectedCalBeforeSave
        {
            get { return CBCalculateBeforeSave.Checked; }
        }

        private bool SelectedIteration
        {
            get { return EnableIteration.Checked; }
        }

        private int SelectedMaxIterations
        {
            get
            {
                int t = Convert.ToInt32(MaxIterations.Text);
                return t;
            }
        }

        private double SelectedMaxChange
        {
            get
            {
                double d = Convert.ToDouble(MaxChange.Text);
                return d;
            }
        }

        #endregion

        private void AddHandler()
        {
            baseButtonPanel1.OkClick += (sender, e) => TriggerSet();
            baseButtonPanel1.ResetClick += (sender, e) => Init();
        }

        public void TriggerInit(IWorkbookSet wbs)
        {
            BookSet = wbs;
            LbwTrigger = LockBookWorker.GetWorker(BookSet);
            Init();
            AddHandler();
        }

        private void Init()
        {
            LbwTrigger.DoWork(() =>
            {
                InitCalculation();
                InitIterations();
            });
        }

        private void TriggerSet()
        {
            LbwTrigger.DoWork(() =>
            {
                SetCalculation();
                SetIterations();
            });
        }

        private void InitCalculation()
        {
            CBCalculateBeforeSave.Checked = BookSet.CalculateBeforeSave;
            switch (BookSet.Calculation)
            {
                case Calculation.Automatic:
                    Automatic.Checked = true;
                    break;
                case Calculation.Manual:
                    Manual.Checked = true;
                    break;
                case Calculation.SemiAutomatic:
                    SemiAutomatic.Checked = true;
                    break;
            }
        }

        private void InitIterations()
        {
            EnableIteration.Checked = BookSet.Iteration;
            MaxChange.Text = BookSet.MaxChange.ToString(CultureInfo.InvariantCulture);
            MaxIterations.Text = BookSet.MaxIterations.ToString();
        }

        private void SetCalculation()
        {
            BookSet.CalculateBeforeSave = SelectedCalBeforeSave;
            BookSet.Calculation = SelectedCalculation;
        }

        private void SetIterations()
        {
            BookSet.Iteration = SelectedIteration;
            BookSet.MaxChange = SelectedMaxChange;
            BookSet.MaxIterations = SelectedMaxIterations;
        }
    }
}